<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\model;

/**
 * Description of GameRecordModel
 *
 * @author SoChishun
 */
class GameRecordModel extends BaseModel {

    //put your code here
    protected $table = 't_game_record';
    protected $autoWriteTimestamp = 'timestamp';
    protected $updateTime = false;

    public function saveRecord($data, $user_id) {
        $rule = [
            'da' => 'number|token',
            'xiao' => 'number',
            'dan' => 'number',
            'shuang' => 'number',
        ];
        $message = [
            'da.token' => '表单来源无效',
            'da.require' => '大数未填写',
            'da.number' => '大数无效',
            'xiao.require' => '小数未填写',
            'xiao.number' => '小数无效',
            'dan.require' => '单数未填写',
            'dan.number' => '单数无效',
            'shuang.require' => '双数未填写',
            'shuang.number' => '双数无效',
            'rate.require' => '倍率未填写',
            'rate.number' => '倍率无效',
        ];
        $validate = \think\Validate::make($rule, $message);
        if (!$validate->check($data)) {
            $error = $validate->getError();
            return ['success' => false, 'message' => $error];
        }
        // 读取游戏配置
        $gameconf = \app\index\model\ConfModel::get(1);
        if (null == $gameconf) {
            return ['success' => false, 'message' => '倍率未配置，请联系管理员!'];
        }
        $currency_rate = $gameconf['currency_rate'];
        $fields = ['da', 'xiao', 'dan', 'shuang'];
        $cost_currency = 0;
        $paycurrencytmp = 0;
        $conffield = [];
        $saveData = [];
        foreach ($fields as $field) {
            $saveData['buy_' . $field] = isset($data[$field]) ? $data[$field] : 0;
        }
        foreach ($fields as $field) {
            $paycurrencytmp = isset($data[$field]) ? ( $data[$field] * $currency_rate) : 0;
            $saveData['pay_currency_' . $field] = $paycurrencytmp;
            $conffield[] = 'return_rate_' . $field;
            $cost_currency += $paycurrencytmp;
        }
        $out_trade_no = 'jc' . date('YmdHis') . rand(1000, 9999);
        $saveData['user_id'] = $user_id;
        $saveData['out_trade_no'] = $out_trade_no;
        $saveData['cost_currency'] = $cost_currency;
        $saveData['currency_rate'] = $currency_rate;
        $saveData = array_merge($saveData, $gameconf->visible($conffield)->toArray());
        if (false === $this->save($saveData)) {
            return ['success' => false, 'message' => $this->getError()];
        }
        return ['success' => true, 'message' => $this->toArray()];
    }

    public function notifyRecord($out_trade_no, $out_pay_no, $user_id) {
        $gamerecord = $this->get(['out_trade_no' => $out_trade_no]);
        if (null == $gamerecord) {
            return ['success' => false, 'message' => '记录不存在!'];
        }

        if (!$gamerecord->out_pay_no) {
            $draw_no = substr($out_pay_no, strlen($out_pay_no) - 2);
            $gamerecord->out_pay_no = $out_pay_no;
            $gamerecord->draw_no = $draw_no;
            $gamerecord->is_large = $draw_no >= 50;
            $gamerecord->is_single = ($draw_no % 2 > 0);
            $win_currency = 0;
            if ($gamerecord->is_large) {
                $return_currency = $gamerecord['return_rate_da'] * $gamerecord['pay_currency_da'];
                $win_currency += $return_currency;
                $gamerecord->return_da = $return_currency;
            } else {
                $return_currency = $gamerecord['return_rate_xiao'] * $gamerecord['pay_currency_xiao'];
                $win_currency += $return_currency;
                $gamerecord->return_xiao = $return_currency;
            }
            if ($gamerecord->is_single) {
                $return_currency = $gamerecord['return_rate_dan'] * $gamerecord['pay_currency_dan'];
                $win_currency += $return_currency;
                $gamerecord->return_dan = $return_currency;
            } else {
                $return_currency = $gamerecord['return_rate_shuang'] * $gamerecord['pay_currency_shuang'];
                $win_currency += $return_currency;
                $gamerecord->return_shuang = $return_currency;
            }
            $gamerecord->win_currency = $win_currency;
            if (!$gamerecord->save([], ['out_trade_no' => $out_trade_no])) {
                return ['success' => false, 'message' => $gamerecord->getError()];
            }
            // 扣除账户余额
            \app\index\model\UserModel::where(['id' => $user_id])->setDec('currency', $gamerecord->cost_currency);
            // 增加账户余额
            \app\index\model\UserModel::where(['id' => $user_id])->setInc('currency', $win_currency);
        }
        $data = $gamerecord->toArray();
        return ['success' => true, 'message' => $data];
    }

    public function getStatusTextAttr() {
        switch ($this->draw_status) {
            case 0:
                return '未开奖';
            case 1:
                return '中奖';
            case 2:
                return '未中奖';
        }
    }

}
